﻿
var interval = 5; //生成间隔
var UrlList = new Array();
var GatherState = -1; //采集状态
var NextCategoryUrl = "";
var StartUrlIndex = 0;
var GatherID = 0;
var ProgressNum;
var ProgressIgnoreNum;
var GatheringNum = 0;
var IgnoreNum = 0;
var thread = 5;
//正常采集
function GatherMain()
{
    $.ajax({
        type: "POST",
        url: "GatherClass.ashx", 
        timeout: 6000000,
        data: {
            IDs: Request.QueryString("IDs"),
            ModelId: Request.QueryString("ModelId"),
            NextCategoryUrl: escape(NextCategoryUrl),
            Reverse: $("#Reverse").is(":checked"),
            StartUrlIndex: StartUrlIndex
        },
        dataType: "json",
        success: function (info)
        {
            if (info.errors != "")
            {
                Stop();
                Message(info.GatheringUrl, "采集任务中止。<br />" + info.errors, -1);
                return;
            }
            NextCategoryUrl = info.NextCategoryUrl;
            StartUrlIndex = info.StartUrlIndex;
            GatherID = info.GatherID;

            Message(info.GatheringUrl, "正在采集" + info.Urls.length + "页。<br />下一栏目页是：" + info.NextCategoryUrl, -1);

            for (var i = 0; i < info.Urls.length; i++)
            {
                UrlList.push(info.Urls[i]);
            }

            //开始采集
            if (GatherState != -1)
                GatherState = 1;
            CurrentRowIndex = 0;
            GatherWeb(GetUrl(CurrentRowIndex));
        },
        error: function (msg)
        {
            Stop();
            Message(null, "内部服务器错误。" + msg.statusText, 0);
        }
    });
}

function GatherWebStart()
{
    if (UrlList.length > 0)
    {
        GatherState = 1;
        CurrentRowIndex = 0;
        GatherWeb(CurrentRowIndex);
    }
}
function GetUrl(index)
{
    var str = '';
    var J = index + thread;
    if (index + thread > UrlList.length)
        J = UrlList.length;

    for (var i = index; i < J; i++)
    {
        str += UrlList[i] + '|';
        CurrentRowIndex = i;
    }
    return str;
}

function GatherWeb(urls)
{
    if (GatherState == -1)
    {
        Message(null, "用户停止采集.", 3);
        return;
    }

    if (urls == "" && NextCategoryUrl != "")
    {
        GoGatheringNextClass();
        return;
    }

    if (urls.length == "" && NextCategoryUrl == "")
    {
        Stop();
        Message(null, "采集完成C.", 3);
        return;
    }


    $.ajax({
        type: "POST",
        url: "GatherWeb.ashx", 
        timeout: 6000000,
        data: {
            ID: GatherID,
            ModelId: Request.QueryString("ModelId"),
            URL: escape(urls),
            BadLinkNum: $("#BadLinkNum").val(),
            IsTest: $("#IsTest").is(":checked"),
            IsUpdate: $("#IsUpdate").is(":checked"),
            IsLeech: $("#IsLeech").is(":checked")
        },
        dataType: "JSON",
        error: function (XMLHttpRequest, textStatus, errorThrown)
        {
            //IsStop();
            Message(null, "错误!" + XMLHttpRequest.statusText, 0);
            Message(null, "错误!" + textStatus.toString(), 0);
            Message(null, "错误!" + errorThrown.toString(), 0);
        },
        success: function (data, textStatus)
        {
            for (var j = 0; j < data.length; j++)
            {
                Message(data[j].URL, data[j].Messages, data[j].Type);

                if (data[j].Gathered == true)
                {
                    GatheringNum++;
                }
                else
                {
                    IgnoreNum++;
                }
            }
            ProgressNum.text(GatheringNum);
            ProgressIgnoreNum.text(IgnoreNum);

            if ((NextCategoryUrl == "" && CurrentRowIndex == UrlList.length - 1))
            {
                Stop();
            }
            if (GatherState == -1 && CurrentRowIndex == 0)
            {
                Message(null, "采集完成A.", -1);
                return;
            }


            if (CurrentRowIndex < UrlList.length - 1)
            {
                CurrentRowIndex++;

                window.setTimeout("GatherWeb(\"" + GetUrl(CurrentRowIndex) + "\")", interval);
            }

            if (NextCategoryUrl != "" && CurrentRowIndex == UrlList.length - 1)
            {
                GoGatheringNextClass();
            }
        }
    });

}

// 重新采集一下页栏目页
function GoGatheringNextClass()
{
    CurrentRowIndex = 0;
    UrlList = new Array();

    GatherMain();
}

function Start()
{
    GatherState = 1;
    UrlList = new Array();

    GatheringNum = 0;
    IgnoreNum = 0;
    StartUrlIndex = 0;
    ProgressNum = $("#ProgressNum");
    ProgressIgnoreNum = $("#ProgressIgnoreNum");
    ProgressNum.text(0);
    ProgressIgnoreNum.text(0);

    thread = parseInt($("#Gatherthread").val());
    $('#btnstart').attr("disabled", true);
    $('#btnstop').removeAttr("disabled");

    Message(null, "开始采集。", 3);

    GatherMain();
}
function Stop()
{
    GatherState = -1;
    CurrentRowIndex = 0;
    UrlList = new Array();
    NextCategoryUrl = "";


    $("#btnstart").removeAttr("disabled");
    $("#btnstop").attr("disabled", true);
}